package edu.cuit.campus.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/15 10:13
 */
@Configuration
@ComponentScan(basePackages = "edu.cuit.campus", excludeFilters = @ComponentScan.Filter(classes = Controller.class))
@PropertySource(value = "classpath:jdbc.properties", encoding = "utf-8")
@MapperScan(basePackages = "edu.cuit.campus.mapper")
@EnableTransactionManagement //开启注解声明式事务
public class SpringRootApplicationContextConfig {
    //配置数据源
    @Bean
    public DataSource dataSource(@Value("${driverClassName}") String driverClassName,
                                 @Value("${url}") String url,
                                 @Value("${jdbc.username}") String username,
                                 @Value("${password}")String password,
                                 @Value("${defaultAutoCommit}")boolean defaultAutoCommit,
                                 @Value("${connectionProperties}")String connectionProperties){

        var dataSource = new DruidDataSource();
        dataSource.setConnectionProperties(connectionProperties);
        dataSource.setUrl(url);
        dataSource.setDefaultAutoCommit(defaultAutoCommit);
        dataSource.setPassword(password);
        dataSource.setUsername(username);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    //配置SqlSessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        var factory = new SqlSessionFactoryBean();
        //setting
        var configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true); //开启驼峰映射
        factory.setConfiguration(configuration);

        //type-aliases
        factory.setTypeAliasesPackage("edu.cuit.campus.po");

        //type-handlers
        factory.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);

        //plugins
        var interceptor = new PageInterceptor();
        var prop = new Properties();
        prop.setProperty("rowBoundsWithCount", "true");
        prop.setProperty("reasonable", "true");
        interceptor.setProperties(prop);
        factory.setPlugins(interceptor);

        //env
        factory.setDataSource(dataSource);

        //mappers = (Mapper接口 + xml映射文件)
        var resources = new PathMatchingResourcePatternResolver().getResources("classpath:edu/cuit/campus/mapper/**/*.xml");
        factory.setMapperLocations(resources);

        return factory.getObject();
    }
    //配置事务管理器
    @Bean
    public TransactionManager txManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

}
